package com.zerui.manager.app.accessauthdevice.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.cola.dto.PageResponse;
import com.zerui.manager.accessauthdevice.AccessAuthDevice;
import com.zerui.manager.accessauthdevice.domainservice.AccessAuthDeviceRepository;
import com.zerui.manager.accessauthorization.AccessAuth;
import com.zerui.manager.accessauthorization.gateway.AccessAuthGateWay;
import com.zerui.manager.devicegroup.DeviceGroup;
import com.zerui.manager.devicegroup.gateway.DeviceGroupGateWay;
import com.zerui.manager.web.cmd.AccessAuthDeviceQryCmd;
import com.zerui.manager.web.vo.AccessAuthDevicePageVO;
import com.zerui.manager.web.vo.AccessAuthDeviceVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Author wh
 * @create_time 2023-08-29 14:35
 */
@Component
@CatchAndLog
public class AccessAuthDeviceQryCmdExe {

	@Resource
	private AccessAuthDeviceRepository accessAuthDeviceRepository;
	@Resource
	private AccessAuthGateWay accessAuthGateWay;
	@Resource
	private DeviceGroupGateWay deviceGroupGateWay;
	@Resource
	private AccessAuthDeviceSyncCmdExe accessAuthDeviceSyncCmdExe;

	public PageResponse<AccessAuthDevicePageVO> execute(AccessAuthDeviceQryCmd accessAuthDeviceQryCmd) {
		AccessAuth accessAuth = accessAuthGateWay.selectById(accessAuthDeviceQryCmd.getAccessAuthId());
		if(ObjectUtils.isEmpty(accessAuth)){
			throw new BizException("门禁授权组不存在");
		}
		Page<AccessAuthDevice> page = accessAuthDeviceRepository.getPageList(accessAuthDeviceQryCmd);
		List<AccessAuthDevice> records = page.getRecords();
		List<AccessAuthDevicePageVO> authDevicePages = new ArrayList<>();

		Map<Long, DeviceGroup> deviceGroupMap = deviceGroupGateWay.getDeviceGroupMapByIds(records
					.stream()
					.map(AccessAuthDevice::getDeviceId)
					.collect(Collectors.toSet()));
		if(CollectionUtils.isNotEmpty(records)){
			records.forEach(e-> authDevicePages.add(AccessAuthDevicePageVO.builder()
					.name(e.getDeviceName())
					.deviceId(e.getDeviceId())
					.groupName(deviceGroupMap.containsKey(e.getDeviceId()) ? deviceGroupMap.get(e.getDeviceId()).getOrgName() : null)
					.position(e.getPosition())
					.accessAuthId(e.getAccessAuthId())
					.type(e.getType())
					.typeName(e.getTypeName())
					.createTime(e.getCreateTime())
					.build()));
		}
		return PageResponse.of(authDevicePages,(int)page.getTotal(),
				(int) page.getSize(),
				(int) page.getCurrent());
	}


	public AccessAuthDeviceVO selectByDeviceId(Long deviceId) {
		AccessAuthDevice authDevice = accessAuthDeviceRepository.selectByDeviceId(deviceId);
		if (null == authDevice) return null;
		Map<Long, DeviceGroup> deviceGroupMap = deviceGroupGateWay.getDeviceGroupMapByIds(Collections.singleton(deviceId));
		return AccessAuthDeviceVO.builder()
				.name(authDevice.getDeviceName()) // 设备名称
				.deviceId(authDevice.getDeviceId()) // 设备ID
				.deviceCode(authDevice.getDeviceCode()) // 设备编号
				.deviceFrom(authDevice.getDeviceFrom())
				.groupName(deviceGroupMap.containsKey(authDevice.getDeviceId()) ? deviceGroupMap.get(authDevice.getDeviceId()).getOrgName() : null)
				.position(authDevice.getPosition())
				.accessAuthId(authDevice.getAccessAuthId())
				.type(authDevice.getType())
				.typeName(authDevice.getTypeName())
				.createTime(authDevice.getCreateTime())
				.build();
	}

	public List<AccessAuthDeviceVO> selectByDeviceIds(Set<Long> deviceIds) {
		List<AccessAuthDevice> authDevices = accessAuthDeviceRepository.selectByDeviceIds(deviceIds);
		if (null == authDevices) return null;
		Map<Long, DeviceGroup> deviceGroupMap = deviceGroupGateWay.getDeviceGroupMapByIds(deviceIds);
		List<AccessAuthDeviceVO> authDeviceVOS = new ArrayList<>();
		authDevices.forEach(e -> authDeviceVOS.add(AccessAuthDeviceVO.builder()
				.name(e.getDeviceName()) // 设备名称
				.deviceId(e.getDeviceId()) // 设备ID
				.deviceCode(e.getDeviceCode()) // 设备编号
				.deviceFrom(e.getDeviceFrom())
				.groupName(deviceGroupMap.containsKey(e.getDeviceId()) ? deviceGroupMap.get(e.getDeviceId()).getOrgName() : null)
				.position(e.getPosition())
				.accessAuthId(e.getAccessAuthId())
				.type(e.getType())
				.typeName(e.getTypeName())
				.createTime(e.getCreateTime())
				.build()));
		return authDeviceVOS;
	}
}
